//
//  HKSwiftMacro.swift
//  HKUtilitySwift
//
//  Created by 胡锦涛 on 2018/12/20.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

import UIKit
import Foundation
import AVFoundation

//MARK: 常用类型定义

/// 默认字典类型
public typealias HKDefaultDic = [String: Any]
/// 不带参数函数
public typealias HKEmptyParameterFunc = () -> Void

///MARK: 设备宽高
public let SCREEN_WIDTH = UIScreen.main.bounds.size.width
public let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
public let SCREEN_BOUNDS = UIScreen.main.bounds
public let SCREENMIN = min(SCREEN_WIDTH, SCREEN_HEIGHT)
public let SCREENMAX = max(SCREEN_WIDTH, SCREEN_HEIGHT)

/// 判断设备
public let HK_IsIphoneX = UIScreen.main.bounds.size.height >= 812

/// 顶部间隙 - 用来区分 是否IPHONE X
public let HK_DEVICE_TOP = (UIScreen.main.bounds.height >= 812 ? 44.0 : 20.0) as CGFloat

/// 导航栏高度获取
public let HK_NAV_HEIGHT = (UIApplication.shared.statusBarFrame.height + 44 ) as CGFloat

///  TABBAR 高度获取
public let HK_TAB_HEIGHT =  (UIScreen.main.bounds.size.height >= 812 ? 83 : 49) as CGFloat

/// 字体缩放
public func gScale(a : CGFloat) -> CGFloat{
    return a * SCREEN_WIDTH / 375.0
}
///Normal字体
public func kFont(fontSize : CGFloat) -> UIFont{
    return UIFont.systemFont(ofSize: fontSize)
}
///加粗字体
public func kBoldFont(fontSize : CGFloat) -> UIFont{
    return UIFont.boldSystemFont(ofSize: fontSize)
}

//MARK:单例
public let HKKeyWindow = UIApplication.shared.keyWindow
public let HKAppdelegate = UIApplication.shared.delegate
public let HKUserDefaults = UserDefaults.standard
public let HKNotiCenter:NotificationCenter = NotificationCenter.default


// MARK: 其他实用
//Bundle
public let HK_BUNDLE:Bundle = Bundle.main
//是否是iPad
public let HK_isPad:Bool  = UIDevice.current.model == "iPad"

//MARK:版本号相关
let HK_INFO = Bundle.main.infoDictionary!
let HK_DisplayName   = HK_INFO["CFBundleDisplayName"]               //程序名称
let HK_ShortVersion  = HK_INFO["CFBundleShortVersionString"]        //主程序版本号
let HK_BuildVersion  = HK_INFO["CFBundleVersion"]                   //版本号（内部标示）
let HK_BundleId      = HK_INFO["CFBundleIdentifier"]                //bundleId
let HK_ProjectName   = HK_INFO["CFBundleExecutable"]                //工程名

///设备信息
let HK_Device_Version = UIDevice.current.systemVersion //iOS版本
let HK_Device_Udid = UIDevice.current.identifierForVendor //设备udid
let HK_Device_SystemName = UIDevice.current.systemName //设备名称
let HK_Device_Model = UIDevice.current.model //设备型号
let HK_Device_Name = UIDevice.current.name //设备具体型号
let HK_Device_LocalizedModel = UIDevice.current.localizedModel //设备区域化型号如A1533


// MARK: 常用颜色设置
//MARK: 颜色
/// rgb获取颜色-不带透明度
public func HKRGB(_ red: CGFloat, _ green: CGFloat , _ blue: CGFloat, alhpa: CGFloat = 1.0) -> UIColor {
    return UIColor.init(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: alhpa)
}

///rgb获取颜色-带透明度
public func HKRGBA (_ r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat)-> UIColor{
    return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}

///随机色的
public func kRandomColor () -> UIColor {
    return UIColor.random
}

///HexColor 支持0xFF55c9c4格式
public func HKHexColor(_ argb: UInt32) -> UIColor {
    return UIColor(red: ((CGFloat)((argb & 0xFF0000) >> 16)) / 255.0,green: ((CGFloat)((argb & 0xFF00) >> 8)) / 255.0,blue: ((CGFloat)(argb & 0xFF)) / 255.0, alpha: 1.0)
}

///HexStyring 支持"#55c9c4"和"55c9c4"格式
public func HKHexString(_ hexString: String) -> UIColor {
    return hexString.toUIColor()
}

///延迟加载 - delay
public func HKDelay(_ delay:Double, closure:@escaping ()->()) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

// MARK: - 图片
public func HKImageNamed(imageName: String) -> UIImage {
    return UIImage.init(named: imageName)!
}

//MARK:颜色其他
public let COLOR_MIGU:UInt32 = 0xFF55C9C4 //星宝主体颜色
public let NAV_BGCOLOR:UInt32 = 0xFF0b0f48 //导航条颜色

public let kRedColor:UIColor = UIColor.red
public let kBlueColor:UIColor = UIColor.blue
public let kGreenColor:UIColor = UIColor.green
public let kYellowColor:UIColor = UIColor.yellow

//MARK: debug打印
public func HKLog<T>(_ message : T,methodName: String = #function, lineNumber: Int = #line){
    #if DEBUG
    print("\(methodName)[\(lineNumber)]:\(message)")
    #endif
}
extension String {
    /// 将十六进制颜色转伟UIColor
    /// - Returns: UIColor
    public func toUIColor() -> UIColor {
        //处理数值
        var cString = self.uppercased().trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        
        let length = (cString as NSString).length
        //错误处理
        if (length < 6 || length > 7 || (!cString.hasPrefix("#") && length == 7)){
            return UIColor.white
        }
        
        if cString.hasPrefix("#"){
            cString = (cString as NSString).substring(from: 1)
        }
        
        //字符chuan截取
        var range = NSRange()
        range.location = 0
        range.length = 2
        
        let rString = (cString as NSString).substring(with: range)
        
        range.location = 2
        let gString = (cString as NSString).substring(with: range)
        
        range.location = 4
        let bString = (cString as NSString).substring(with: range)
        
        //存储转换后的数值
        var r:UInt32 = 0,g:UInt32 = 0,b:UInt32 = 0
        //进行转换
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        //根据颜色值创建UIColor
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
    }
}

class HKSwiftMacro: NSObject {
    
    static let scale : Double = {
        return Double(SCREENMIN/750.0)
    }()
    
    static let shared = HKSwiftMacro()
    
    var dateFormate = DateFormatter()
    
    var today : String {
        dateFormate.dateFormat = "yyyy-MM-dd"
        return dateFormate.string(from: Date())
    }
    
    var navbarHeight : CGFloat {
        get{
            if HK_IsIphoneX{return 88}
            return 64
        }set{}
    }
    
    var tabbarHeight : CGFloat {
        get{
            if HK_IsIphoneX{return 83}
            return 49
        }set{}
    }
    
    var statusbarHeight : CGFloat{
        get{
            if HK_IsIphoneX{return 44}
            return 20
        }set{}
    }
    
    //设置日期显示格式
    public  func  formatDate(_ date:Date, _ dateStr:String) -> String{
        let formatter:DateFormatter = DateFormatter.init()
        formatter.dateFormat = dateStr
        return formatter.string(from: date as Date)
    }
    
    public func getTimeStamp(sinceNow:TimeInterval) -> String{
        
        let date = Date(timeIntervalSinceNow: sinceNow)
        let a = date.timeIntervalSince1970*1000
        let timeS = NSString(format: "%.0f", a)
        return timeS as String
    }
    
    func getSystemVolume() -> Float {
        return AVAudioSession.sharedInstance().outputVolume
    }
}
